package com.gill.touka.exception;

import com.gill.touka.exception.msg.ErrorBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * spring mvc提供的全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionErrorHandler {

    @ExceptionHandler({SecurityException.class})
    public ResponseEntity<ErrorBody> error(SecurityException e){
        log.warn("发生SecurityException异常", e);
        return new ResponseEntity<>(ErrorBody.builder()
                .body(e.getMessage())
                .status(HttpStatus.UNAUTHORIZED.value())
                .build(),
                HttpStatus.UNAUTHORIZED);
    }
}
